#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <iostream>

using namespace std;

int main()
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    server_addr.sin_port = htons(8888);

    bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));

    struct sockaddr_in client_addr;
    socklen_t addr_len = sizeof(client_addr);

    char buf[1024];
    while (1)
    {
        recvfrom(sockfd, buf, 1024, 0, (struct sockaddr *)&client_addr, &addr_len);
        cout << buf << endl;
        cout << "ip: " << inet_ntoa(client_addr.sin_addr)
             << ", port: " << ntohs(client_addr.sin_port) << endl;
    }
}